package com.example.cuibo.loginvc;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cuibo on 2017/11/6.
 */

public class UserInfoUtils {

    public static boolean saveInfo(Context context, String userName, String pwd) {

        try {
            String path = context.getFilesDir().getPath();
            File file = new File(path,"info.txt");
            String result = userName + "##" + pwd;
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(result.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
    }
    // 读取用户的信息
    public static Map<String,String> readInfo(Context context) {
        // 定义Map
       try {Map<String,String> maps = new HashMap<>();
           String path = context.getFilesDir().getPath();
           File file = new File(path,"info.txt");
        FileInputStream fis = new FileInputStream(file);
        BufferedReader buf = new BufferedReader(new InputStreamReader(fis));
           String content = buf.readLine();

//           切割字符串
           String[] splits = content.split("##");
           String name = splits[0];
           String pwd = splits[1];
           maps.put("name",name);
           maps.put("pwd",pwd);
           fis.close();
       }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
